﻿using System;
using System.IO;
using System.Runtime.Serialization;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using SymulatorEthernetWPF.Shared;
using SymulatorEthernetWPF.UserInterface;
using SymulatorEthernetWPF.SimObjects.Layers.Physical;
using SymulatorEthernetWPF.SimObjects.Layers.DataLink;
using System.Windows;
using System.Collections.Generic;

namespace SymulatorEthernetWPF.SimObjects.Devices
{
    [Serializable]
    internal class HubControl : DeviceControl
    {
        private List<DelayTimer> timers = new List<DelayTimer>();

        public HubControl()
        {
            icon.Source = StaticVars.hubISource;
            deviceLabel.Content = "HUB " + Simulation.HubCounter;
            using (var stream = new MemoryStream())
            {
                var encoder = new PngBitmapEncoder { Interlace = PngInterlaceOption.On };
                if (icon.Source != null) encoder.Frames.Add(BitmapFrame.Create((BitmapSource)icon.Source));
                encoder.Save(stream);
                RawImageData = stream.ToArray();
            }
            Simulation.HubCounter++;
        }

        public HubControl(SerializationInfo info, StreamingContext context)
            : base(info, context)
        {
        }

        internal override void startReceiving(Signal signal, NetworkInterface networkInterface)
        {
            DelayTimer passingDelay = new DelayTimer(10000.0 * (1.0 / (double)networkInterface.wireEnd.getConnectionType()), networkInterface, signal);
            passingDelay.Start();
            passingDelay.Tick += passingDelay_Tick;
            timers.Add(passingDelay);

        }

        void passingDelay_Tick(object sender, EventArgs e)
        {
            NetworkInterface networkInterface = ((DelayTimer)sender).networkInterface;
            Signal signal = ((DelayTimer)sender).signal;
            ((DelayTimer)sender).Stop();
            timers.Remove((DelayTimer)sender);
            foreach (NetworkInterface it in interfaces)
            {
                if (it.wireEnd != null && it != networkInterface)
                {
                    if (!signal.isJam)
                    {
                        it.sendFrame(signal.ethernet);
                    }
                    else
                    {
                        it.wireEnd.stopSignals();
                        it.sendJam();
                    }
                }
            }
        }


        internal override void stopReceiving(Signal signal, NetworkInterface networkInterface)
        {
            return;
        }

        internal override void abortReceiving(Signal signal, NetworkInterface networkInterface)
        {
            foreach (NetworkInterface it in interfaces)
                it.abortEthernetFrame(signal.ethernet);
        }


        public override void sendFrame(Ethernet frameData)
        {
            
            foreach (NetworkInterface it in interfaces)
            {
                if (it.wireEnd != null)
                {
                    //frameData.source = it.Adress;
                    it.sendFrame(frameData);
                }
            }
        }

        protected override void DeviceControlContextMenuPropertiesClick(object sender, RoutedEventArgs e)
        {
            (new HubSettings(this)).ShowDialog();
        }

        internal override void reset()
        {
            foreach (DelayTimer it in timers)
            {
                it.Stop();
            }
            timers.Clear();
        }
    }
}